package com.mytijian.openapi.adapter.alihalt.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mytijian.openapi.adapter.alihalt.AlihaltMcodeHelper;
import com.mytijian.openapi.adapter.alihalt.consts.AlihaltConsts;
import com.mytijian.openapi.adapter.alihalt.exception.AlihaltException;
import com.mytijian.openapi.adapter.alihalt.model.McodeInfo;
import com.mytijian.openapi.adapter.alihalt.param.AlihaltCounterParam;
import com.mytijian.openapi.adapter.alihalt.result.AlihaltCounterInfo;
import com.mytijian.openapi.adapter.alihalt.result.AlihaltCounterResult;
import com.mytijian.openapi.adapter.alihalt.service.AlihaltCounterService;
import com.mytijian.openapi.core.client.IOpenAPIClient;
import com.mytijian.openapi.core.client.result.CounterResult;
import com.mytijian.openapi.core.model.AppConfig;
import com.mytijian.openapi.core.service.OpenApiAppConfigService;

@Service
public class AlihaltCounterServiceImpl implements AlihaltCounterService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private OpenApiAppConfigService openApiService;
	
	@Autowired
	private IOpenAPIClient client;
	
	@Autowired
	private AlihaltMcodeHelper mcodeHelper;

	@Override
	public AlihaltCounterResult getCounter(AlihaltCounterParam param) {
		
		log.info("[阿里健康] [人数查询] 入参: {}", JSON.toJSONString(param));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date toDay = new Date();
		Date lastDay = new Date(System.currentTimeMillis()+(90*86400*1000L));
		
		String startDate = sdf.format(toDay);
		String endDate =  sdf.format(lastDay);
		
//		if (StringUtils.isEmpty(param.getCheckUnitCode()) 
//				|| !StringUtils.isNumeric(param.getCheckUnitCode())) {
//			log.info("[阿里健康] [人数查询] 参数校验错误。");
//			throw new AlihaltException("400", "体检中心ID checkUnitCode 必须是一个数字，实际传入["+param.getCheckUnitCode()+"]");
//		}
		
		// 
		//Integer hospitalId = Integer.valueOf( param.getCheckUnitCode() );
		
		McodeInfo mcodeInfo = mcodeHelper.getMealId(param.getServiceNumber());
		
		AppConfig config = openApiService.getConfig(AlihaltConsts.AppName);
		
		CounterResult counterResult = client.getCounter(config, mcodeInfo.getRefHospitalId(), startDate, endDate);
		
		if (StringUtils.isNotEmpty(counterResult.getErrCode())) {
			log.error("[阿里对接] 调用人数接口失败 开放平台返回码 {}", JSON.toJSONString(counterResult));
			throw new AlihaltException("500", "开放平台接口调用失败");
		}
		
		AlihaltCounterResult result = new AlihaltCounterResult();
		result.setResponseCode("200");
		AlihaltCounterInfo info = new AlihaltCounterInfo();
		BeanUtils.copyProperties(counterResult, info);
		
		List<AlihaltCounterInfo> ll = new ArrayList<>();
		ll.add(info);
		result.setResult(ll);
		
		log.info("[阿里健康] [人数查询] 返回数据条数: {}", ll.size());
		return result;
	}

}
